﻿using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Model.Manager;
using System.Data;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class HomeController : BaseController
    {
        [Description("首页")]
        [NoVerify]
        public ActionResult Index()
        {
            var user = new CurrentUser();
            string str = string.Empty;

            if (user != null && user.user != null && user.user.uid != 0)
            {
                //从缓存中获取用户菜单
                List<MenuInfo> menulist = DataCacheHelper.Get<List<MenuInfo>>(user.user.uid.ToString());
                //通过linq表达式查找菜单中的父菜单
                var parentmenu = from n in menulist
                                 where n.parentID == 0
                                 orderby n.menuId ascending
                                 select n;
                int i = 0;
                var isselect = "";
                if (parentmenu != null)
                {
                    foreach (var it in parentmenu)
                    {
                        //判断菜单是否可见
                        if (it.isVisible == 0)
                        {
                            if (i == 0)
                                isselect = " dl-selected";
                            else
                                isselect = "";
                            i++;

                            if (it.menuCode == "System")
                            {
                                str += "<li class=\"nav-item " + isselect + "\"><div class=\"nav-item-inner nav-home\">" + it.menuName + "</div></li>";
                            }
                            else
                            {
                                str += "<li class=\"nav-item " + isselect + "\"><div class=\"nav-item-inner nav-order\">" + it.menuName + "</div></li>";
                            }
                        }
                    }
                }
                user.user.TopStr = str;
            }

            return View(user.user);
        }


        [Description("错误提示")]
        [NoVerify]
        public ActionResult Error()
        {
            return View();
        }

        [Description("权限问题提示")]
        [NoVerify]
        public ActionResult Problem()
        {
            return View();
        }

        [Description("加载左侧菜单")]
        [NoVerify]
        public ActionResult GetMenuConfig(long uid)
        {
            List<MainConfigModel> list = new List<MainConfigModel>();
            if (uid != 0)
            {
                //从缓存中获取用户菜单
                List<MenuInfo> menulist = DataCacheHelper.Get<List<MenuInfo>>(uid.ToString());

                //通过linq表达式查找菜单中的父菜单
                var parentmenu = from n in menulist
                                 where n.parentID == 0
                                 orderby n.menuId ascending
                                 select n;

                if (parentmenu != null)
                {
                    foreach (var it in parentmenu)
                    {
                        //判断菜单是否可见
                        if (it.isVisible == 0)
                        {
                            MainConfigModel config = new MainConfigModel();
                            config.id = it.menuId.ToString();

                            //获取二级菜单
                            var childmenu = from nn in menulist
                                            where nn.parentID == it.menuId
                                            orderby nn.menuId ascending
                                            select nn;

                            if (childmenu != null)
                            {
                                List<MainMenuModel> menumodel = new List<MainMenuModel>();

                                foreach (var twoitem in childmenu)
                                {
                                    //获取三级菜单
                                    var threemenu = from nnn in menulist
                                                    where nnn.parentID == twoitem.menuId
                                                    orderby nnn.menuId ascending
                                                    select nnn;

                                    MainMenuModel mmm = new MainMenuModel();
                                    mmm.text = twoitem.menuName;
                                    List<MainMenuItems> menuitems = new List<MainMenuItems>();

                                    foreach (var threeitem in threemenu)
                                    {
                                        //判断菜单是否可见
                                        if (threeitem.isVisible == 0)
                                        {
                                            MainMenuItems mm = new MainMenuItems();
                                            mm.id = threeitem.menuId.ToString();
                                            mm.text = threeitem.menuName;
                                            mm.href = threeitem.menuUrl;

                                            menuitems.Add(mm);
                                        }
                                        mmm.items = menuitems;

                                    }

                                    menumodel.Add(mmm);
                                    config.menu = menumodel;
                                }
                                if (config.menu != null && config.menu[0].items != null && config.menu[0].items.Count != 0)
                                {
                                    config.homePage = config.menu[0].items[0].id;
                                }
                            }
                            list.Add(config);
                        }
                    }
                }
            }
            return Json(list);
        }

    }
}